package com.itheima.reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectDemo2 {

    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {
        // 获取类的信息
        // 1. 获取类的本身
        System.out.println("获取类的信息");
        Class c = Student.class;
        System.out.println(c.getName());
        System.out.println(c.getSimpleName());

        System.out.println("----------------------------");
        // 2. 获取类的构造器对象
        System.out.println("获取类的构造器对象");
        Constructor [] constructors = c.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
        // 获得单个构造器对象
        Constructor constructor = c.getConstructor(String.class, int.class);
        System.out.println(constructor);

        System.out.println("----------------------------");
        // 3. 获取类的成员变量
        System.out.println("获取类的成员变量");
        Field [] fields = c.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field.getName() + ":" + field.getType());
        }
        // 获得单个成员变量
        Field field = c.getDeclaredField("age");
        System.out.println(field.getName() + ":" + field.getType());

        System.out.println("----------------------------");
        // 4. 获取类的成员方法
        System.out.println("获取类的成员方法");
        Method [] methods = c.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method.getName() + ":" + method.getReturnType());
        }
        // 获得单个成员方法
        Method method = c.getDeclaredMethod("getName");
        System.out.println(method.getName() + ":" + method.getReturnType());

    }
}
